home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7991 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Perf:] mem*() procs vs. array looping
  5. Date: 28 Feb 1996 12:24:29 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h2dltINNlag@anvil.ugrad.cs.ubc.ca>
  8. References: <4glkq1$gu7@gazette.tandem.com> <4h1n14$3b3@news.interpath.net>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h1n14$3b3@news.interpath.net>,
  12. Scott McMahan - Softbase Systems <softbase@mercury.interpath.net> wrote:
  13.  >Francis E. Chang (francis@patch.tandem.com) wrote:
  14.  >
  15.  >: Are mem*() procedures performance boosters?
  16.  >
  17.  >I have had some experience with this exact question, and want to 
  18.  >share what I found out.
  19.  >
  20.  >1. The only real answer is, "it depends". It depends on who wrote the
  21.  >stdlib routines, how conscientious they were, how much they knew about
  22.  >the hardware they were writing on, etc. From one library to the
  23.  >next, the answer could change.
  24.  
  25. What makes you think that memcpy() and friends are necessarily function calls
  26. to library routines? Since they are standard defined functions, the compiler
  27. has the license to generate inline code for a reference to them, provided that
  28. they have not been redefined as internal objects (that is, static functions)
  29. inside the translation unit in the scope of the reference, and you are using a
  30. hosted C environment. You can do such redefinition of a standard function such
  31. as memcpy() if you don't #include the header which declares it as an extern
  32. object, and if you write it as a static.  In that case, the compiler will
  33. generate code that calls _your_ memcpy() rather than make inline code which
  34. implements the standard memcpy().
  35.  
  36.  >2. The only way to tell is on a per-stdlib basis! Measuring execution
  37.  >of the program in a proflier for every different memxxx in every
  38.  >different library. You *have* to profile the program and see what
  39.  >is going on. Does calling memxxx even matter in the overall scheme?
  40.  
  41. You don't have to profile. This is far too simple to require profiling.
  42. Timing a large number of memcpy() operations should be quite telling.
  43.  
  44.  >3. Most if not all commercial compilers will write things like memcpy
  45.  >in assembly language, and some processors have native instructions for
  46.  >doing memory copies and stuff that make them much faster than any C
  47.  >code you could write, because they don't have to continually load
  48.  >addresses like you do in a loop.
  49.  
  50. Not only that, but some machines have special co-processors for doing these
  51. kinds of blits (e.g. Amiga, some Suns).
  52.  
  53.  >4. I had a program where memset was THE bottleneck, taking up more time
  54.  >than I/O. I re-wrote a memory zeroing function using the most unrolled,
  55.  >efficient loop I could write, and it was still orders of magnitude
  56.  >slower than the system memset.  No way I could make it faster in C.
  57.  
  58. The compiler may have generated an in-line code utilizing the best idiom for
  59. the architecture. Wins hands down, unless you spoil the standard compliance of
  60. your code with your own explicit inline assembly language that is even better.
  61. Generally not worth it.
  62.  
  63.  >5. WRT #4, I removed the memset. It was initializing a buffer, and I
  64.  >said "forget it" and used the uninitialized buffer and took my chances.
  65.  >Changing the design was more effecient than coding hokey
  66.  >optimizations! After removing memset, I/O functions accounted for
  67.  >90% or more of the time spent in the program, which I could live with.
  68.  
  69. Did you assume anything about the uninitialized contents? That is never a good
  70. thing for auto or dynamically allocated storage. Though if you need the speed,
  71. you can always try sacrificing portability.
  72.  
  73.  >Scott
  74.  >
  75. -- 
  76.  
  77.